/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;

import dto.ExcelDTO;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import poi.engine.ExcelEngine;

/**
 *
 * @author techworld
 */
public class FileUploadScreen extends java.awt.Frame {

    /**
     * Creates new form FileUploadScreen
     */
    
    String filePath = null;
    public FileUploadScreen() {
        initComponents();
        buttonFunction.setVisible(false);
    }
    
    ExcelEngine engine = new ExcelEngine();

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelHeader = new javax.swing.JLabel();
        textFieldPath = new javax.swing.JTextField();
        buttonUpload = new javax.swing.JButton();
        labelFooter = new javax.swing.JLabel();
        buttonFunction = new javax.swing.JButton();
        textLabel = new javax.swing.JTextField();

        setResizable(false);
        setTitle("Huge Excel File Engine");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });
        setLayout(null);

        labelHeader.setFont(new java.awt.Font("Courier 10 Pitch", 1, 24)); // NOI18N
        labelHeader.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelHeader.setText("File Uploader Screen");
        add(labelHeader);
        labelHeader.setBounds(0, 30, 590, 30);
        add(textFieldPath);
        textFieldPath.setBounds(20, 120, 450, 25);

        buttonUpload.setText("Upload");
        buttonUpload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonUploadActionPerformed(evt);
            }
        });
        add(buttonUpload);
        buttonUpload.setBounds(480, 120, 110, 30);

        labelFooter.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelFooter.setText("Technologies Unlimited -  Prashant Kumar");
        add(labelFooter);
        labelFooter.setBounds(120, 200, 370, 15);

        buttonFunction.setText("Start");
        buttonFunction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonFunctionActionPerformed(evt);
            }
        });
        add(buttonFunction);
        buttonFunction.setBounds(240, 150, 150, 29);

        textLabel.setEditable(false);
        textLabel.setText("Please choose excel file.");
        add(textLabel);
        textLabel.setBounds(20, 90, 570, 25);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Exit the Application
     */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }//GEN-LAST:event_exitForm

    private void buttonUploadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonUploadActionPerformed
       JFileChooser uploadFile = new JFileChooser();
       uploadFile.showOpenDialog(null);
       try{
            File fileName  =  uploadFile.getSelectedFile();
            filePath = fileName.getAbsolutePath();
            
            System.out.println("filePath: "+filePath);
            System.out.println("fileParentPath: "+fileName.getParent());
            System.out.println("fileName: "+fileName.getName());       
            
            ExcelDTO.setFilePath(filePath);
            ExcelDTO.setFileName(fileName.getName());
            ExcelDTO.setFileParentPath(fileName.getParent());
                       
            if(!fileName.getName().contains(".xls")){
                textFieldPath.setText("Invalid File Selected");
                buttonFunction.setVisible(false);
            }
            else{
                textFieldPath.setText(filePath);
                buttonFunction.setVisible(true);
            }
       }
       catch(NullPointerException npe){
           textFieldPath.setText("No File Selected");
           buttonFunction.setVisible(false);
       }
       catch(Exception e){
           textFieldPath.setText("Unexpected error accoured please call: +91-9910954457");
           buttonFunction.setVisible(false);
       }
    }//GEN-LAST:event_buttonUploadActionPerformed

    private void buttonFunctionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonFunctionActionPerformed
        System.out.println("Inside Function");
        engine.doReadExcelFile();
        String divString = JOptionPane.showInputDialog(null, "Enter the number of data you want per sheet.\nTotal number row in the excel sheet "+ExcelDTO.getRowNum()+" :\nPlease enter number less than or equal to total row.", "Excel Engine", 1);
        if(divString==null){
            textFieldPath.setText("Invalid number entered: null, Click on Start again");
        }
        else if(divString.equalsIgnoreCase("") || divString.isEmpty()){
            textFieldPath.setText("Invalid number entered, Click on Start again");
        }
        else{
            int divNumber  = Integer.parseInt(divString);
            if(divNumber>ExcelDTO.getRowNum() || divNumber<0){
                textFieldPath.setText("Invalid number entered:"+divNumber+", Click on Start again");
            }
            else{
                textLabel.setText("Please Wait..");
                buttonUpload.setVisible(false);
                buttonFunction.setVisible(false);
                textFieldPath.setText("Creating new excel file started, Please wait...");
                ExcelDTO.setDivNum(divNumber);
                engine.doWriteExcelFile();
            }
        }
        
    }//GEN-LAST:event_buttonFunctionActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FileUploadScreen().setVisible(true);
            }
        });
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonFunction;
    private javax.swing.JButton buttonUpload;
    private javax.swing.JLabel labelFooter;
    private javax.swing.JLabel labelHeader;
    private javax.swing.JTextField textFieldPath;
    private javax.swing.JTextField textLabel;
    // End of variables declaration//GEN-END:variables
}
